package iedcyt.acute.dataAccess.dao.maauco;

import iedcyt.acute.dataAccess.domain.maauco.Au0;
import iedcyt.acute.dataAccess.generic.SpringDao;
import iedcyt.acute.dataAccess.idao.maauco.IAu0Dao;

import java.util.List;



/**
 * Data Access Object para la tabla Au0
 * @author 53385255R
 *
 */
public class Au0Dao extends SpringDao implements IAu0Dao  {

	/**
	 * @see iedcyt.acute.dataAccess.idao.maauco.IAu0Dao#create()
	 */
	@Override
	public void create() {
		//comprobamos si la tabla existe y si no la creamos
		try {
			_jdbcTemplate.queryForInt("select count(*) from Au0");
		} catch (Exception e) {
			String sqlins = "CREATE TABLE Au0 (idno numeric, no numeric, auth varchar(100))";
			String sqlidx = "CREATE INDEX idxau0 on Au0 (idno, no) WITH PRIMARY";
			_log.info(sqlins);
			_log.info(sqlidx);
			_jdbcTemplate.execute(sqlins);
			_jdbcTemplate.execute(sqlidx);
			
		}
		//Creamos indices
		try {
			String sqlidxAu0 = "CREATE INDEX idxAu0Auth ON Au0 (Auth)";
			_log.info(sqlidxAu0);
			_jdbcTemplate.execute(sqlidxAu0);
		} catch (Exception e) {
			_log.error("Error creando indices en Au0 y Co0",e);
		}
	}
	
	@Override
	public List<Au0> selectAll() {
		
		return _jdbcTemplate.query("SELECT * FROM Au0",
				new Au0RowMapper());
	}
	
	@Override
	public void deleteAll() {
		
		_jdbcTemplate.update("DELETE from Au0");
	}

	

	@Override
	public void insert(Au0 doc) {
	
		_jdbcTemplate.update("INSERT INTO Au0 (idno, no, auth) VALUES (?,?,?)",
				new Object[] { doc.get_idno(), doc.get_no(),doc.get_auth() });
		
	}
	
	@Override
	public void update(Au0 doc) {
	
		_jdbcTemplate.update("UPDATE Au0 SET  AUTH = ?, NO = ? WHERE idno = ?",
				new Object[] { doc.get_auth(), doc.get_no(), doc.get_idno() });
		
	}

	
	
	@Override
	public void delete(int idno, int no) {
	
		_jdbcTemplate.update("DELETE from Au0 WHERE idno=? AND NO=?",
				new Object[] { idno, no  });
		
	}

	

	

	
	@Override
	public List<Au0> selectByAuth(String auth) {
		return _jdbcTemplate
		.query(
				"SELECT * FROM Au0 WHERE AUTH = ?",
				new Object[] { auth },
				new Au0RowMapper());
	}

	@Override
	public List<Au0> selectByIdno(int idno) {
		return _jdbcTemplate
		.query(
				"SELECT * FROM Au0 WHERE IDNO = ?",
				new Object[] { idno },
				new Au0RowMapper());
	}

	@Override
	public List<Au0> selectByIdnoRange(int idno1,
			int idno2) {
		return _jdbcTemplate
		.query(
				"SELECT * FROM Au0 WHERE IDNO >= ? AND IDNO <= ?",
				new Object[] { idno1, idno2 },
				new Au0RowMapper());
	}

	@Override
	public List<Au0> select(int idno, int no) {
		return _jdbcTemplate
		.query(
				"SELECT * FROM Au0 WHERE IDNO = ? AND NO = ?",
				new Object[] { idno, no },
				new Au0RowMapper());
	}

	@Override
	public int selectMaxIdno() {
		return _jdbcTemplate.queryForInt("SELECT MAX(IDNO) FROM Au0");
	}

}

